package 多线程练习;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class test3ABC {
      public static void main(String[] args) {
            A a = new A();
            Thread t1 = new Thread(a,"A");
            Thread t2 = new Thread(a,"B");
            t1.start();
            t2.start();

      }
}

class A extends Thread {
      //交替打印1-100
      Object o = new Object();

      AtomicInteger atomicInteger=new AtomicInteger(1);

      ReentrantLock lock=new ReentrantLock();

      @Override
      public void run() {
            synchronized (o) {
                  while (atomicInteger.get() <= 100) {
                        o.notify();
                        System.out.println(Thread.currentThread().getName() + "正在打印" +atomicInteger.get());
                        atomicInteger.getAndIncrement();
                        try {
                              o.wait();
                        } catch (InterruptedException e) {
                              throw new RuntimeException(e);
                        }
                  }
            }
      }
}
